/**
 * 
 */
package edi.android.tmdbBrowser;

import java.util.ArrayList;
import java.util.List;

import net.sf.jtmdb.Movie;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;

/**
 * Activity to show a list of movie search results
 * 
 * @author edi
 * 
 */
public class MovieListActivity extends Activity {
	private List<Movie> displayMovies, searchResults;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Bundle extras = getIntent().getExtras();
		setTitle(getString(R.string.movieListSearchResults) + " \""
				+ extras.getString("searchString") + "\"");
		
		// get first 10 search results, if we got 10
		searchResults=TMDbApp.movieDownloader.getLastMovieResult();
		int maxnum=10;
		if (maxnum>searchResults.size()) {
			maxnum=searchResults.size();
		}
		displayMovies = new ArrayList<Movie>();
		for (int i = 0; i<maxnum; i++) {
			displayMovies.add(searchResults.get(i));
		}
		ListView listView = new ListView(getBaseContext());
		listView.setAdapter(new MovieAdapter(getBaseContext(), displayMovies,
				(TMDbApp) getApplication()));
		this.setContentView(listView);
		listView.setOnItemClickListener(new OnItemClickListener() {
			// when clicking on a movie, show the details of the movie
			@SuppressWarnings("unchecked")
			public void onItemClick(AdapterView parent, View v, int position,
					long id) {
				Intent intent = new Intent(getBaseContext(), ShowMovieActivity.class);
				intent.putExtra("movieID", (int) id);
				startActivity(intent);
			}
		});
	}

}
